package com.atstudy.controller;

import com.atstudy.entites.Account;
import com.atstudy.service.AccountService;
import com.atstudy.task.AsyncTask;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @version v1.0
 * @description:
 * @author: admin
 * @date: 2023/2/15 11:12
 * @copyright: 博为峰
 */
//@RestController
@Controller
@RequestMapping("/account")
@Slf4j
public class AccountController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private AsyncTask asyncTask;

    @GetMapping(value = "/getAccountListJson")
    public List<Account> getAccountListJson() {
        List<Account> accountList = accountService.getAccountList();
        log.debug("accountList:{}", accountList);
        return accountList;
    }

    @GetMapping(value = "/getAccountList")
    public PageInfo<Account> getAccountList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Account> accountList = accountService.getAccountList();
        PageInfo<Account> pageInfo = new PageInfo<>(accountList);
        //异步
        System.out.println(Thread.currentThread().getName());
        asyncTask.async();
//        async();
        return pageInfo;
    }

    @GetMapping(value = "/getAccountListPage")
    public String getAccountListPage(ModelMap map, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Account> accountList = accountService.getAccountList();
        PageInfo<Account> pageInfo = new PageInfo<>(accountList);
        map.addAttribute("pageInfo", pageInfo);
        return "account";
    }

    /*@Async
    public void async() {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName());
    }*/

}
